﻿using Mail.MiniProgram.Core.Helpers;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace Mail.MiniProgram.Core.WorkWechat.Message
{
    public class MessageApi
    {

        public const string API_URL = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={0}";

        /// <summary>
        /// 发送文本信息
        /// </summary>
        /// <param name="accessTokenOrAppKey">调用接口凭证（AccessToken）或AppKey（根据AccessTokenContainer.BuildingKey(corpId, corpSecret)方法获得）</param>
        /// <param name="toUser">UserID列表（消息接收者，多个接收者用‘|’分隔）。特殊情况：指定为@all，则向关注该企业应用的全部成员发送</param>
        /// <param name="toParty">PartyID列表，多个接受者用‘|’分隔。当touser为@all时忽略本参数</param>
        /// <param name="toTag">TagID列表，多个接受者用‘|’分隔。当touser为@all时忽略本参数</param>
        /// <param name="agentId">企业应用的id，可在应用的设置页面查看</param>
        /// <param name="content">消息内容</param>
        /// <param name="safe">表示是否是保密消息，0表示否，1表示是，默认0</param>
        /// <param name="timeOut">代理请求超时时间（毫秒）</param>
        /// <returns></returns>
        public static MessageResult SendText(string accessToken, string agentId, string content,
            string toUser = null, string toParty = null, string toTag = null, int safe = 0)
        {
            var data = new
            {
                touser = toUser,
                toparty = toParty,
                totag = toTag,
                msgtype = "text",
                agentid = agentId,
                text = new
                {
                    content = content
                },
                safe = safe
            };

            string url = string.Format(API_URL, accessToken);

            return HttpHelper.Post<MessageResult>(url, data);

        }



        /// <summary>
        /// 发送小程序应用信息
        /// </summary>
        /// <param name="accessToken"></param>
        /// <param name="appid"></param>
        /// <param name="toUser"></param>
        /// <param name="title"></param>
        /// <param name="contentList"></param>
        /// <param name="description"></param>
        /// <param name="emphasis_first_item"></param>
        /// <returns></returns>
        public static MessageResult SendText(string accessToken, string appid, string toUser,
            string title, List<Tuple<string, string>> contentList, string description = "", bool emphasis_first_item = true
       )
        {
            var data = new
            {
                touser = toUser,
                msgtype = "miniprogram_notice",
                miniprogram_notice = new
                {
                    appid = appid,
                    title = title,
                    description = description,
                    emphasis_first_item = emphasis_first_item,
                    content_item = contentList.Select(
                                   n => new
                                   {
                                       key = n.Item1,
                                       value = n.Item2
                                   }
                        )
                }
            };

            string url = string.Format(API_URL, accessToken);

            return HttpHelper.Post<MessageResult>(url, data);

        }
    }
}
